﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HierarchicalClustering.Metrics;
using TextMining;

namespace HierarchicalClustering.LinkageCriteria
{
    public class Min : ILinkage
    {
        #region ILinkage Members

        public double Distance(Cluster clusterA, Cluster clusterB, IMetric metric)
        {
            // min{ d(a,b }
            double min = -1, temp;

            // the distance between 2 clusters is the distance between 2 of their documents
            // which have the minimum distance

            foreach (Document docA in clusterA.Documents)
            {
                foreach (Document docB in clusterB.Documents)
                {
                    temp = metric.Distance(docA, docB);
                    if (min == -1 || temp < min)
                        min = temp;
                }
            }

            return min;
        }

        public double AddWeight(Cluster clusterA, Cluster clusterB, double distance)
        {   // min{ d(a,b) }
            return distance;
        }

        public double NewDistance(double distanceA, double distanceB)
        {    // min{ d(a,b) } = min{ min{ d(a,b) } = min{ min{ d(a1,b1) }, min{ d(a2,b2) } }
            return Math.Min(distanceA, distanceB);
        }

        #endregion
    }
}
